package com.hspedu.newData.dmsxl.数组.二分查找;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC704二分查找 {

    public int search(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;
        int result = -1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if (nums[mid] > target){
                right = mid - 1;
            }else if (nums[mid] < target){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return result;
    }

    @Test
    public void test(){
//        int search = search(new int[]{-1, 0, 3, 5, 9, 12}, 9);
        int search1 = search(new int[]{-1,0,3,5,9,12}, 13);
        System.out.println(search1);
    }
}
